home *** CD-ROM | disk | FTP | other *** search
/ Linux Cubed Series 2: Applications / Linux Cubed Series 2 - Applications.iso / editors / emacs / xemacs / xemacs-1.006 / xemacs-1 / lib / xemacs-19.13 / lisp / packages / xscheme.el < prev    next >
Encoding:
Text File  |  1995-08-29  |  41.4 KB  |  1,162 lines

  1. ;;; xscheme.el --- run Scheme under Emacs
  2. ;; Keywords: languages, lisp
  3.  
  4. ;; Copyright (C) 1986, 1987 Free Software Foundation, Inc.
  5.  
  6. ;; This file is part of XEmacs.
  7.  
  8. ;; XEmacs is free software; you can redistribute it and/or modify it
  9. ;; under the terms of the GNU General Public License as published by
  10. ;; the Free Software Foundation; either version 2, or (at your option)
  11. ;; any later version.
  12.  
  13. ;; XEmacs is distributed in the hope that it will be useful, but
  14. ;; WITHOUT ANY WARRANTY; without even the implied warranty of
  15. ;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
  16. ;; General Public License for more details.
  17.  
  18. ;; You should have received a copy of the GNU General Public License
  19. ;; along with XEmacs; see the file COPYING.  If not, write to the Free
  20. ;; Software Foundation, 675 Mass Ave, Cambridge, MA 02139, USA.
  21.  
  22. ;;; Requires C-Scheme release 5 or later
  23. ;;; Changes to Control-G handler require runtime version 13.85 or later
  24.  
  25. ;;; $Header: /scheme/etc/RCS/xscheme.el,v 1.32 1993/11/16 15:58:23 gjr Exp $
  26.  
  27. (require 'scheme)
  28.  
  29. ;;;###autoload
  30. (defvar scheme-program-name "scheme"
  31.   "*Program invoked by the `run-scheme' command.")
  32.  
  33. ;;;###autoload
  34. (defvar scheme-band-name nil
  35.   "*Band loaded by the `run-scheme' command.")
  36.  
  37. ;;;###autoload
  38. (defvar scheme-program-arguments nil
  39.   "*Arguments passed to the Scheme program by the `run-scheme' command.")
  40.  
  41. (defvar xscheme-allow-pipelined-evaluation t
  42.   "If non-nil, an expression may be transmitted while another is evaluating.
  43. Otherwise, attempting to evaluate an expression before the previous expression
  44. has finished evaluating will signal an error.")
  45.  
  46. (defvar default-xscheme-runlight
  47.   '(": " xscheme-runlight-string)
  48.   "Default global (shared) xscheme-runlight modeline format.")
  49.  
  50. (defvar xscheme-startup-message
  51.   "This is the Scheme process buffer.
  52. Type \\[advertised-xscheme-send-previous-expression] to evaluate the expression before point.
  53. Type \\[xscheme-send-control-g-interrupt] to abort evaluation.
  54. Type \\[describe-mode] for more information.
  55.  
  56. "
  57.   "String to insert into Scheme process buffer first time it is started.
  58. Is processed with `substitute-command-keys' first.")
  59.  
  60. (defvar xscheme-signal-death-message nil
  61.   "If non-nil, causes a message to be generated when the Scheme process dies.")
  62.  
  63. (defvar xscheme-process-name "*scheme*"
  64.   "*Process created by the `run-scheme' command.")
  65.  
  66. (defvar xscheme-buffer-name "*scheme*"
  67.   "*Buffer created by the `run-scheme' command.")
  68.  
  69. (defun xscheme-evaluation-commands (keymap)
  70.   (define-key keymap "\e\C-x" 'xscheme-send-definition)
  71.   (define-key keymap "\C-x\C-e" 'advertised-xscheme-send-previous-expression)
  72.   (define-key keymap "\eo" 'xscheme-send-buffer)
  73.   (define-key keymap "\ez" 'xscheme-send-definition)
  74.   (define-key keymap "\e\C-m" 'xscheme-send-previous-expression)
  75.   (define-key keymap "\e\C-z" 'xscheme-send-region))
  76.  
  77. (defun xscheme-interrupt-commands (keymap)
  78.   (define-key keymap "\C-c\C-s" 'xscheme-select-process-buffer)
  79.   (define-key keymap "\C-c\C-b" 'xscheme-send-breakpoint-interrupt)
  80.   (define-key keymap "\C-c\C-c" 'xscheme-send-control-g-interrupt)
  81.   (define-key keymap "\C-c\C-u" 'xscheme-send-control-u-interrupt)
  82.   (define-key keymap "\C-c\C-x" 'xscheme-send-control-x-interrupt))
  83.  
  84. (xscheme-evaluation-commands scheme-mode-map)
  85. (xscheme-interrupt-commands scheme-mode-map)
  86.  
  87. ;;;###autoload
  88. (defun run-scheme (command-line)
  89.   "Run an inferior Scheme process.
  90. Output goes to the buffer `*scheme*'.
  91. With argument, asks for a command line."
  92.   (interactive
  93.    (list (read-scheme-command-line current-prefix-arg)))
  94.   (scheme-start command-line xscheme-process-name xscheme-buffer-name))
  95.  
  96. (defun reset-scheme ()
  97.   "Reset the Scheme process."
  98.   (interactive)
  99.   (let ((process (get-process xscheme-process-name)))
  100.     (cond ((or (not process)
  101.            (not (eq (process-status process) 'run))
  102.            (yes-or-no-p
  103. "The Scheme process is running, are you SURE you want to reset it? "))
  104.        (message "Resetting Scheme process...")
  105.        (if process
  106.            (progn
  107.         (kill-process process t)
  108.         (delete-process process)))
  109.        (xscheme-start-process xscheme-process-command-line
  110.                   xscheme-process-name
  111.                   xscheme-buffer-name)
  112.        (message "Resetting Scheme process...done")))))
  113.  
  114. (defun scheme-start (command-line process-name buffer-name &optional avoid-set)
  115.   (if (not avoid-set)
  116.       (setq-default xscheme-process-command-line command-line))
  117.   (switch-to-buffer
  118.    (xscheme-start-process command-line process-name buffer-name))
  119.   (make-local-variable 'xscheme-process-command-line)
  120.   (setq xscheme-process-command-line command-line))
  121.  
  122. (defun read-scheme-command-line (arg)
  123.   (let ((default
  124.       (or xscheme-process-command-line
  125.           (xscheme-default-command-line))))
  126.     (if arg
  127.     (read-string "Run Scheme: " default)
  128.       default)))
  129.  
  130. (defun xscheme-default-command-line ()
  131.   (concat scheme-program-name " -emacs"
  132.       (if scheme-program-arguments
  133.           (concat " " scheme-program-arguments)
  134.           "")
  135.       (if scheme-band-name
  136.           (concat " -band " scheme-band-name)
  137.           "")))
  138.  
  139. ;;;; Multiple Scheme buffer management commands
  140.  
  141. (defun start-scheme (buffer-name &optional globally)
  142.   "Choose a scheme interaction buffer, or create a new one."
  143.   ;; (interactive "BScheme interaction buffer: \nP")
  144.   (interactive
  145.    (list (read-buffer "Scheme interaction buffer: "
  146.               xscheme-buffer-name
  147.               nil)
  148.      current-prefix-arg))
  149.   (let ((buffer (get-buffer-create buffer-name)))
  150.     (let ((process (get-buffer-process buffer)))
  151.       (if process
  152.       (switch-to-buffer buffer)
  153.     (if (or (not (buffer-file-name buffer))
  154.         (yes-or-no-p (concat "Buffer "
  155.                      (buffer-name buffer)
  156.                      " contains file "
  157.                      (buffer-file-name buffer)
  158.                      "; start scheme in it? ")))
  159.         (progn
  160.           (scheme-start (read-scheme-command-line t)
  161.                 buffer-name
  162.                 buffer-name)
  163.           (if globally
  164.           (global-set-scheme-interaction-buffer buffer-name)))
  165.       (message "start-scheme aborted"))))))
  166.  
  167. (fset 'select-scheme 'start-scheme)
  168.  
  169. (defun global-set-scheme-interaction-buffer (buffer-name)
  170.   "Set the default scheme interaction buffer."
  171.   (interactive
  172.    (list (read-buffer "Scheme interaction buffer: "
  173.               xscheme-buffer-name
  174.               t)))
  175.   (let ((process-name (verify-xscheme-buffer buffer-name nil)))
  176.     (setq-default xscheme-buffer-name buffer-name)
  177.     (setq-default xscheme-process-name process-name)
  178.     (setq-default xscheme-runlight-string
  179.           (save-excursion (set-buffer buffer-name)
  180.                   xscheme-runlight-string))
  181.     (setq-default xscheme-runlight
  182.           (if (eq (process-status process-name) 'run)
  183.               default-xscheme-runlight
  184.             ""))))
  185.  
  186. (defun local-set-scheme-interaction-buffer (buffer-name)
  187.   "Set the scheme interaction buffer for the current buffer."
  188.   (interactive
  189.    (list (read-buffer "Scheme interaction buffer: "
  190.               xscheme-buffer-name
  191.               t)))
  192.   (let ((process-name (verify-xscheme-buffer buffer-name t)))
  193.     (make-local-variable 'xscheme-buffer-name)
  194.     (setq xscheme-buffer-name buffer-name)
  195.     (make-local-variable 'xscheme-process-name)
  196.     (setq xscheme-process-name process-name)
  197.     (make-local-variable 'xscheme-runlight)
  198.     (setq xscheme-runlight (save-excursion (set-buffer buffer-name)
  199.                        xscheme-runlight))))
  200.  
  201. (defun local-clear-scheme-interaction-buffer ()
  202.   "Make the current buffer use the default scheme interaction buffer."
  203.   (interactive)
  204.   (if (xscheme-process-buffer-current-p)
  205.       (error "Cannot change the interaction buffer of an interaction buffer"))
  206.   (kill-local-variable 'xscheme-buffer-name)
  207.   (kill-local-variable 'xscheme-process-name)
  208.   (kill-local-variable 'xscheme-runlight))
  209.   
  210. (defun verify-xscheme-buffer (buffer-name localp)
  211.   (if (and localp (xscheme-process-buffer-current-p))
  212.       (error "Cannot change the interaction buffer of an interaction buffer"))
  213.   (let* ((buffer (get-buffer buffer-name))
  214.      (process (and buffer (get-buffer-process buffer))))
  215.     (cond ((not buffer)
  216.        (error "Buffer does not exist" buffer-name))
  217.       ((not process)
  218.        (error "Buffer is not a scheme interaction buffer" buffer-name))
  219.       (t
  220.        (save-excursion
  221.          (set-buffer buffer)
  222.          (if (not (xscheme-process-buffer-current-p))
  223.          (error "Buffer is not a scheme interaction buffer"
  224.             buffer-name)))
  225.        (process-name process)))))
  226.  
  227. ;;;; Interaction Mode
  228.  
  229. (defun scheme-interaction-mode (&optional preserve)
  230.   "Major mode for interacting with the inferior Scheme process.
  231. Like  scheme-mode  except that:
  232.  
  233. \\[advertised-xscheme-send-previous-expression] sends the expression before point to the Scheme process as input
  234. \\[xscheme-yank-pop] yanks an expression previously sent to Scheme
  235. \\[xscheme-yank-push] yanks an expression more recently sent to Scheme
  236.  
  237. All output from the Scheme process is written in the Scheme process
  238. buffer, which is initially named \"*scheme*\".  The result of
  239. evaluating a Scheme expression is also printed in the process buffer,
  240. preceded by the string \";Value: \" to highlight it.  If the process
  241. buffer is not visible at that time, the value will also be displayed
  242. in the minibuffer.  If an error occurs, the process buffer will
  243. automatically pop up to show you the error message.
  244.  
  245. While the Scheme process is running, the modelines of all buffers in
  246. scheme-mode are modified to show the state of the process.  The
  247. possible states and their meanings are:
  248.  
  249. input        waiting for input
  250. run        evaluating
  251. gc        garbage collecting
  252.  
  253. The process buffer's modeline contains additional information where
  254. the buffer's name is normally displayed: the command interpreter level
  255. and type.
  256.  
  257. Scheme maintains a stack of command interpreters.  Every time an error
  258. or breakpoint occurs, the current command interpreter is pushed on the
  259. command interpreter stack, and a new command interpreter is started.
  260. One example of why this is done is so that an error that occurs while
  261. you are debugging another error will not destroy the state of the
  262. initial error, allowing you to return to it after the second error has
  263. been fixed.
  264.  
  265. The command interpreter level indicates how many interpreters are in
  266. the command interpreter stack.  It is initially set to one, and it is
  267. incremented every time that stack is pushed, and decremented every
  268. time it is popped.  The following commands are useful for manipulating
  269. the command interpreter stack:
  270.  
  271. \\[xscheme-send-breakpoint-interrupt]    pushes the stack once
  272. \\[xscheme-send-control-u-interrupt]    pops the stack once
  273. \\[xscheme-send-control-g-interrupt]    pops everything off
  274. \\[xscheme-send-control-x-interrupt]    aborts evaluation, doesn't affect stack
  275.  
  276. Some possible command interpreter types and their meanings are:
  277.  
  278. [Evaluator]    read-eval-print loop for evaluating expressions
  279. [Debugger]    single character commands for debugging errors
  280. [Where]        single character commands for examining environments
  281.  
  282. Starting with release 6.2 of Scheme, the latter two types of command
  283. interpreters will change the major mode of the Scheme process buffer
  284. to scheme-debugger-mode , in which the evaluation commands are
  285. disabled, and the keys which normally self insert instead send
  286. themselves to the Scheme process.  The command character ? will list
  287. the available commands.
  288.  
  289. For older releases of Scheme, the major mode will be be
  290. scheme-interaction-mode , and the command characters must be sent as
  291. if they were expressions.
  292.  
  293. Commands:
  294. Delete converts tabs to spaces as it moves back.
  295. Blank lines separate paragraphs.  Semicolons start comments.
  296. \\{scheme-interaction-mode-map}
  297.  
  298. Entry to this mode calls the value of scheme-interaction-mode-hook
  299. with no args, if that value is non-nil.
  300.  Likewise with the value of scheme-mode-hook.
  301.  scheme-interaction-mode-hook is called after scheme-mode-hook."
  302.  
  303.   (interactive "P")
  304.   (if (not preserve)
  305.       (let ((previous-mode major-mode))
  306.         (kill-all-local-variables)
  307.         (make-local-variable 'xscheme-previous-mode)
  308.         (make-local-variable 'xscheme-buffer-name)
  309.         (make-local-variable 'xscheme-process-name)
  310.         (make-local-variable 'xscheme-previous-process-state)
  311.         (make-local-variable 'xscheme-runlight-string)
  312.         (make-local-variable 'xscheme-runlight)
  313.         (setq xscheme-previous-mode previous-mode)
  314.         (let ((buffer (current-buffer)))
  315.           (setq xscheme-buffer-name (buffer-name buffer))
  316.           (let ((process (get-buffer-process buffer)))
  317.             (if (not process)
  318.                 (setq xscheme-previous-process-state (cons nil nil))
  319.                 (progn
  320.                   (setq xscheme-process-name (process-name process))
  321.                   (setq xscheme-previous-process-state
  322.                         (cons (process-filter process)
  323.                               (process-sentinel process)))
  324.           (xscheme-process-filter-initialize t)
  325.           (xscheme-modeline-initialize xscheme-buffer-name)
  326.           (set-process-sentinel process 'xscheme-process-sentinel)
  327.           (set-process-filter process 'xscheme-process-filter)))))))
  328.   (scheme-interaction-mode-initialize)
  329.   (scheme-mode-variables)
  330.   (run-hooks 'scheme-mode-hook 'scheme-interaction-mode-hook))
  331.  
  332. (defun exit-scheme-interaction-mode ()
  333.   "Take buffer out of scheme interaction mode"
  334.   (interactive)
  335.   (if (not (eq major-mode 'scheme-interaction-mode))
  336.       (error "Buffer not in scheme interaction mode"))
  337.   (let ((previous-state xscheme-previous-process-state))
  338.     (funcall xscheme-previous-mode)
  339.     (let ((process (get-buffer-process (current-buffer))))
  340.       (if process
  341.       (progn
  342.         (if (eq (process-filter process) 'xscheme-process-filter)
  343.         (set-process-filter process (car previous-state)))
  344.         (if (eq (process-sentinel process) 'xscheme-process-sentinel)
  345.         (set-process-sentinel process (cdr previous-state))))))))    
  346.  
  347. (defun scheme-interaction-mode-initialize ()
  348.   (use-local-map scheme-interaction-mode-map)
  349.   (setq major-mode 'scheme-interaction-mode)
  350.   (setq mode-name "Scheme Interaction"))
  351.  
  352. (defun scheme-interaction-mode-commands (keymap)
  353.   (define-key keymap "\C-c\C-m" 'xscheme-send-current-line)
  354.   (define-key keymap "\C-c\C-p" 'xscheme-send-proceed)
  355.   (define-key keymap "\C-c\C-y" 'xscheme-yank)
  356.   (define-key keymap "\ep" 'xscheme-yank-pop)
  357.   (define-key keymap "\en" 'xscheme-yank-push))
  358.  
  359. (defvar scheme-interaction-mode-map nil)
  360. (if (not scheme-interaction-mode-map)
  361.     (progn
  362.       (setq scheme-interaction-mode-map (make-keymap))
  363.       (scheme-mode-commands scheme-interaction-mode-map)
  364.       (xscheme-interrupt-commands scheme-interaction-mode-map)
  365.       (xscheme-evaluation-commands scheme-interaction-mode-map)
  366.       (scheme-interaction-mode-commands scheme-interaction-mode-map)))
  367.  
  368. (defun xscheme-enter-interaction-mode ()
  369.   (save-excursion
  370.     (set-buffer (xscheme-process-buffer))
  371.     (if (not (eq major-mode 'scheme-interaction-mode))
  372.     (if (eq major-mode 'scheme-debugger-mode)
  373.         (scheme-interaction-mode-initialize)
  374.         (scheme-interaction-mode t)))))
  375.  
  376. (fset 'advertised-xscheme-send-previous-expression
  377.       'xscheme-send-previous-expression)
  378.  
  379. ;;;; Debugger Mode
  380.  
  381. (defun scheme-debugger-mode ()
  382.   "Major mode for executing the Scheme debugger.
  383. Like  scheme-mode  except that the evaluation commands
  384. are disabled, and characters that would normally be self inserting are
  385. sent to the Scheme process instead.  Typing ?  will show you which
  386. characters perform useful functions.
  387.  
  388. Commands:
  389. \\{scheme-debugger-mode-map}"
  390.   (error "Illegal entry to scheme-debugger-mode"))
  391.  
  392. (defun scheme-debugger-mode-initialize ()
  393.   (use-local-map scheme-debugger-mode-map)
  394.   (setq major-mode 'scheme-debugger-mode)
  395.   (setq mode-name "Scheme Debugger"))
  396.  
  397. (defun scheme-debugger-mode-commands (keymap)
  398.   (let ((char ? ))
  399.     (while (< char 127)
  400.       (define-key keymap (char-to-string char) 'scheme-debugger-self-insert)
  401.       (setq char (1+ char)))))
  402.  
  403. (defvar scheme-debugger-mode-map nil)
  404. (if (not scheme-debugger-mode-map)
  405.     (progn
  406.       (setq scheme-debugger-mode-map (make-keymap))
  407.       (scheme-mode-commands scheme-debugger-mode-map)
  408.       (xscheme-interrupt-commands scheme-debugger-mode-map)
  409.       (scheme-debugger-mode-commands scheme-debugger-mode-map)))
  410.  
  411. (defun scheme-debugger-self-insert ()
  412.   "Transmit this character to the Scheme process."
  413.   (interactive)
  414.   (xscheme-send-char last-command-char))
  415.  
  416. (defun xscheme-enter-debugger-mode (prompt-string)
  417.   (save-excursion
  418.     (set-buffer (xscheme-process-buffer))
  419.     (if (not (eq major-mode 'scheme-debugger-mode))
  420.     (progn
  421.       (if (not (eq major-mode 'scheme-interaction-mode))
  422.           (scheme-interaction-mode t))
  423.       (scheme-debugger-mode-initialize)))))
  424.  
  425. (defun xscheme-debugger-mode-p ()
  426.   (let ((buffer (xscheme-process-buffer)))
  427.     (and buffer
  428.      (save-excursion
  429.        (set-buffer buffer)
  430.        (eq major-mode 'scheme-debugger-mode)))))
  431.  
  432. ;;;; Evaluation Commands
  433.  
  434. (defun xscheme-send-string (&rest strings)
  435.   "Send the string arguments to the Scheme process.
  436. The strings are concatenated and terminated by a newline."
  437.   (cond ((not (xscheme-process-running-p))
  438.      (if (yes-or-no-p "The Scheme process has died.  Reset it? ")
  439.          (progn
  440.            (reset-scheme)
  441.            (xscheme-wait-for-process)
  442.            (xscheme-send-string-1 strings))))
  443.     ((xscheme-debugger-mode-p) (error "No sends allowed in debugger mode"))
  444.     ((and (not xscheme-allow-pipelined-evaluation)
  445.           xscheme-running-p)
  446.      (error "No sends allowed while Scheme running"))
  447.     (t (xscheme-send-string-1 strings))))
  448.  
  449. (defun xscheme-send-string-1 (strings)
  450.   (let ((string (apply 'concat strings)))
  451.     (xscheme-send-string-2 string)
  452.     (if (eq major-mode 'scheme-interaction-mode)
  453.     (xscheme-insert-expression string))))
  454.  
  455. (defun xscheme-send-string-2 (string)
  456.   (let ((process (get-process xscheme-process-name)))
  457.     (process-send-string process (concat string "\n"))
  458.     (if (xscheme-process-buffer-current-p)
  459.     (set-marker (process-mark process) (point)))))
  460.  
  461. (defun xscheme-select-process-buffer ()
  462.   "Select the Scheme process buffer and move to its output point."
  463.   (interactive)
  464.   (let ((process (or (get-process xscheme-process-name)
  465.              (error "No scheme process"))))
  466.     (let ((buffer (or (process-buffer process) (error "No process buffer"))))
  467.       (let ((window (get-buffer-window buffer)))
  468.     (if window
  469.         (select-window window)
  470.         (switch-to-buffer buffer))
  471.     (goto-char (process-mark process))))))
  472.  
  473. ;;;; Scheme expressions ring
  474.  
  475. (defun xscheme-insert-expression (string)
  476.   (setq xscheme-expressions-ring (cons string xscheme-expressions-ring))
  477.   (if (> (length xscheme-expressions-ring) xscheme-expressions-ring-max)
  478.       (setcdr (nthcdr (1- xscheme-expressions-ring-max) xscheme-expressions-ring) nil))
  479.   (setq xscheme-expressions-ring-yank-pointer xscheme-expressions-ring))
  480.  
  481. (defun xscheme-rotate-yank-pointer (arg)
  482.   "Rotate the yanking point in the kill ring."
  483.   (interactive "p")
  484.   (let ((length (length xscheme-expressions-ring)))
  485.     (if (zerop length)
  486.     (error "Scheme expression ring is empty")
  487.       (setq xscheme-expressions-ring-yank-pointer
  488.         (let ((index (% (+ arg (- length (length xscheme-expressions-ring-yank-pointer)))
  489.                 length)))
  490.         (nthcdr (if (< index 0)
  491.             (+ index length)
  492.               index)
  493.             xscheme-expressions-ring))))))
  494.  
  495. (defun xscheme-yank (&optional arg)
  496.   "Insert the most recent expression at point.
  497. With just C-U as argument, same but put point in front (and mark at end).
  498. With argument n, reinsert the nth most recently sent expression.
  499. See also the commands \\[xscheme-yank-pop] and \\[xscheme-yank-push]."
  500.   (interactive "*P")
  501.   (xscheme-rotate-yank-pointer (if (listp arg) 0
  502.                  (if (eq arg '-) -1
  503.                    (1- arg))))
  504.   (push-mark (point))
  505.   (insert (car xscheme-expressions-ring-yank-pointer))
  506.   (if (consp arg)
  507.       (exchange-point-and-mark)))
  508.  
  509. ;; Old name, to avoid errors in users' init files.
  510.  
  511. (fset 'xscheme-yank-previous-send
  512.       'xscheme-yank)
  513.  
  514. (defun xscheme-yank-pop (arg)
  515.   "Insert or replace a just-yanked expression with an older expression.
  516. If the previous command was not a yank, it yanks.
  517. Otherwise, the region contains a stretch of reinserted
  518. expression.  yank-pop deletes that text and inserts in its
  519. place a different expression.
  520.  
  521. With no argument, the next older expression is inserted.
  522. With argument n, the n'th older expression is inserted.
  523. If n is negative, this is a more recent expression.
  524.  
  525. The sequence of expressions wraps around, so that after the oldest one
  526. comes the newest one."
  527.   (interactive "*p")
  528.   (setq this-command 'xscheme-yank)
  529.   (if (not (eq last-command 'xscheme-yank))
  530.       (progn
  531.     (xscheme-yank)
  532.     (setq arg (- arg 1))))
  533.   (if (not (= arg 0))
  534.       (let ((before (< (point) (mark t))))
  535.     (delete-region (point) (mark t))
  536.     (xscheme-rotate-yank-pointer arg)
  537.     (set-mark (point))
  538.     (insert (car xscheme-expressions-ring-yank-pointer))
  539.     (if before (exchange-point-and-mark)))))
  540.  
  541. (defun xscheme-yank-push (arg)
  542.   "Insert or replace a just-yanked expression with a more recent expression.
  543. If the previous command was not a yank, it yanks.
  544. Otherwise, the region contains a stretch of reinserted
  545. expression.  yank-pop deletes that text and inserts in its
  546. place a different expression.
  547.  
  548. With no argument, the next more recent expression is inserted.
  549. With argument n, the n'th more recent expression is inserted.
  550. If n is negative, a less recent expression is used.
  551.  
  552. The sequence of expressions wraps around, so that after the oldest one
  553. comes the newest one."
  554.   (interactive "*p")
  555.   (xscheme-yank-pop (- 0 arg)))
  556.  
  557. (defun xscheme-send-region (start end)
  558.   "Send the current region to the Scheme process.
  559. The region is sent terminated by a newline."
  560.   (interactive "r")
  561.   (if (xscheme-process-buffer-current-p)
  562.       (progn (goto-char end)
  563.          (set-marker (process-mark (get-process xscheme-process-name))
  564.              end)))
  565.   (xscheme-send-string (buffer-substring start end)))
  566.  
  567. (defun xscheme-send-definition ()
  568.   "Send the current definition to the Scheme process.
  569. If the current line begins with a non-whitespace character,
  570. parse an expression from the beginning of the line and send that instead."
  571.   (interactive)
  572.   (let ((start nil) (end nil))
  573.     (save-excursion
  574.       (end-of-defun)
  575.       (setq end (point))
  576.       (if (re-search-backward "^\\s(" nil t)
  577.       (setq start (point))
  578.       (error "Can't find definition")))
  579.     (xscheme-send-region start end)))
  580.  
  581. (defun xscheme-send-next-expression ()
  582.   "Send the expression to the right of `point' to the Scheme process."
  583.   (interactive)
  584.   (let ((start (point)))
  585.     (xscheme-send-region start (save-excursion (forward-sexp) (point)))))
  586.  
  587. (defun xscheme-send-previous-expression ()
  588.   "Send the expression to the left of `point' to the Scheme process."
  589.   (interactive)
  590.   (let ((end (point)))
  591.     (xscheme-send-region (save-excursion (backward-sexp) (point)) end)))
  592.  
  593. (defun xscheme-send-current-line ()
  594.   "Send the current line to the Scheme process.
  595. Useful for working with debugging Scheme under adb."
  596.   (interactive)
  597.   (let ((line
  598.      (save-excursion
  599.        (beginning-of-line)
  600.        (let ((start (point)))
  601.          (end-of-line)
  602.          (buffer-substring start (point))))))
  603.     (end-of-line)
  604.     (insert ?\n)
  605.     (xscheme-send-string-2 line)))
  606.  
  607. (defun xscheme-send-buffer ()
  608.   "Send the current buffer to the Scheme process."
  609.   (interactive)
  610.   (if (xscheme-process-buffer-current-p)
  611.       (error "Not allowed to send this buffer's contents to Scheme"))
  612.   (xscheme-send-region (point-min) (point-max)))
  613.  
  614. (defun xscheme-send-char (char)
  615.   "Prompt for a character and send it to the Scheme process."
  616.   (interactive "cCharacter to send: ")
  617.   (process-send-string xscheme-process-name (char-to-string char)))
  618.  
  619. ;;;; Interrupts
  620.  
  621. (defun xscheme-send-breakpoint-interrupt ()
  622.   "Cause the Scheme process to enter a breakpoint."
  623.   (interactive)
  624.   (xscheme-send-interrupt ?b nil))
  625.  
  626. (defun xscheme-send-proceed ()
  627.   "Cause the Scheme process to proceed from a breakpoint."
  628.   (interactive)
  629.   (process-send-string xscheme-process-name "(proceed)\n"))
  630.  
  631. (defun buffer-local-value-cell (buffer name)
  632.   (let ((pair (assq name (buffer-local-variables (get-buffer buffer)))))
  633.     (if (not pair)
  634.     (error "buffer-local-value-cell: Not bound")
  635.       pair)))
  636.  
  637. (defun xscheme-send-control-g-interrupt ()
  638.   "Cause the Scheme processor to halt and flush input.
  639. Control returns to the top level rep loop."
  640.   (interactive)
  641.   (let* ((inhibit-quit t)
  642.      (vcell (buffer-local-value-cell xscheme-buffer-name
  643.                      'xscheme-control-g-disabled-p)))
  644.     (cond ((not xscheme-control-g-synchronization-p)
  645.        (interrupt-process xscheme-process-name))
  646.       ((cdr vcell)
  647.        (message "Relax..."))
  648.       (t
  649.        (rplacd vcell t)
  650.        (message "Sending C-G interrupt to Scheme...")
  651.        (interrupt-process xscheme-process-name)
  652.        (process-send-string xscheme-process-name (char-to-string 0))))))
  653.  
  654. (defun xscheme-send-control-u-interrupt ()
  655.   "Cause the Scheme process to halt, returning to previous rep loop."
  656.   (interactive)
  657.   (xscheme-send-interrupt ?u t))
  658.  
  659. (defun xscheme-send-control-x-interrupt ()
  660.   "Cause the Scheme process to halt, returning to current rep loop."
  661.   (interactive)
  662.   (xscheme-send-interrupt ?x t))
  663.  
  664. ;;; This doesn't really work right -- Scheme just gobbles the first
  665. ;;; character in the input.  There is no way for us to guarantee that
  666. ;;; the argument to this procedure is the first char unless we put
  667. ;;; some kind of marker in the input stream.
  668.  
  669. (defun xscheme-send-interrupt (char mark-p)
  670.   "Send a ^A type interrupt to the Scheme process."
  671.   (interactive "cInterrupt character to send: ")
  672.   (quit-process xscheme-process-name)
  673.   (process-send-string xscheme-process-name (char-to-string char))
  674.   (if (and mark-p xscheme-control-g-synchronization-p)
  675.       (process-send-string xscheme-process-name (char-to-string 0))))
  676.  
  677. ;;;; Internal Variables
  678.  
  679. (defvar xscheme-process-command-line nil
  680.   "Command used to start the most recent Scheme process.")
  681.  
  682. (defvar xscheme-expressions-ring-max 30
  683.   "*Maximum length of Scheme expressions ring.")
  684.  
  685. (defvar xscheme-expressions-ring nil
  686.   "List of expressions recently transmitted to the Scheme process.")
  687.  
  688. (defvar xscheme-expressions-ring-yank-pointer nil
  689.   "The tail of the Scheme expressions ring whose car is the last thing yanked.")
  690.  
  691. (defvar xscheme-process-filter-state 'idle
  692.   "State of scheme process escape reader state machine:
  693. idle                   waiting for an escape sequence
  694. reading-type           received an altmode but nothing else
  695. reading-string         reading prompt string")
  696.  
  697. (defvar xscheme-running-p nil
  698.   "This variable, if nil, indicates that the scheme process is
  699. waiting for input.  Otherwise, it is busy evaluating something.")
  700.  
  701. (defconst xscheme-control-g-synchronization-p t
  702.   "If non-nil, insert markers in the scheme input stream to indicate when
  703. control-g interrupts were signalled.  Do not allow more control-g's to be
  704. signalled until the scheme process acknowledges receipt.")
  705.  
  706. (defvar xscheme-control-g-disabled-p nil
  707.   "This variable, if non-nil, indicates that a control-g is being processed
  708. by the scheme process, so additional control-g's are to be ignored.")
  709.  
  710. (defvar xscheme-allow-output-p t
  711.   "This variable, if nil, prevents output from the scheme process
  712. from being inserted into the process-buffer.")
  713.  
  714. (defvar xscheme-prompt ""
  715.   "The current scheme prompt string.")
  716.  
  717. (defvar xscheme-string-accumulator ""
  718.   "Accumulator for the string being received from the scheme process.")
  719.  
  720. (defvar xscheme-string-receiver nil
  721.   "Procedure to send the string argument from the scheme process.")
  722.  
  723. (defvar xscheme-start-hook nil
  724.   "If non-nil, a procedure to call when the Scheme process is started.
  725. When called, the current buffer will be the Scheme process-buffer.")
  726.  
  727. (defvar xscheme-runlight "")
  728. (defvar xscheme-runlight-string nil)
  729. (defvar xscheme-mode-string nil)
  730. (setq-default scheme-mode-line-process
  731.           '("" xscheme-runlight))
  732.  
  733. (mapcar 'make-variable-buffer-local
  734.     '(xscheme-expressions-ring
  735.       xscheme-expressions-ring-yank-pointer
  736.       xscheme-process-filter-state
  737.       xscheme-running-p
  738.       xscheme-control-g-disabled-p
  739.       xscheme-allow-output-p
  740.       xscheme-prompt
  741.       xscheme-string-accumulator
  742.       xscheme-mode-string
  743.       scheme-mode-line-process))
  744.  
  745. ;;;; Basic Process Control
  746.  
  747. (defun xscheme-start-process (command-line the-process the-buffer)
  748.   (let ((buffer (get-buffer-create the-buffer)))
  749.     (let ((process (get-buffer-process buffer)))
  750.       (save-excursion
  751.     (set-buffer buffer)
  752.     (if (and process (memq (process-status process) '(run stop)))
  753.         (set-marker (process-mark process) (point-max))
  754.         (progn (if process (delete-process process))
  755.            (goto-char (point-max))
  756.            (scheme-interaction-mode nil)
  757.            (setq xscheme-process-name the-process)
  758.            (if (bobp)
  759.                (insert-before-markers
  760.             (substitute-command-keys xscheme-startup-message)))
  761.            (setq process
  762.              (let ((process-connection-type nil))
  763.                (apply 'start-process
  764.                   (cons the-process
  765.                     (cons buffer
  766.                           (xscheme-parse-command-line
  767.                            command-line))))))
  768.            (if (not (equal (process-name process) the-process))
  769.                (setq xscheme-process-name (process-name process)))
  770.            (if (not (equal (buffer-name buffer) the-buffer))
  771.                (setq xscheme-buffer-name (buffer-name buffer)))
  772.            (message "Starting process %s in buffer %s"
  773.                 xscheme-process-name
  774.                 xscheme-buffer-name)
  775.            (set-marker (process-mark process) (point-max))
  776.            (xscheme-process-filter-initialize t)
  777.            (xscheme-modeline-initialize xscheme-buffer-name)
  778.            (set-process-sentinel process 'xscheme-process-sentinel)
  779.            (set-process-filter process 'xscheme-process-filter)
  780.            (run-hooks 'xscheme-start-hook)))))
  781.     buffer))
  782.  
  783. (defun xscheme-parse-command-line (string)
  784.   (setq string (substitute-in-file-name string))
  785.   (let ((start 0)
  786.     (result '()))
  787.     (while start
  788.       (let ((index (string-match "[ \t]" string start)))
  789.     (setq start
  790.           (cond ((not index)
  791.              (setq result
  792.                (cons (substring string start)
  793.                  result))
  794.              nil)
  795.             ((= index start)
  796.              (string-match "[^ \t]" string start))
  797.             (t
  798.              (setq result
  799.                (cons (substring string start index)
  800.                  result))
  801.              (1+ index))))))
  802.     (nreverse result)))
  803.  
  804. (defun xscheme-wait-for-process ()
  805.   (sleep-for 2)
  806.   (while xscheme-running-p
  807.     (sleep-for 1)))
  808.  
  809. (defun xscheme-process-running-p ()
  810.   "True iff there is a Scheme process whose status is `run'."
  811.   (let ((process (get-process xscheme-process-name)))
  812.     (and process
  813.      (eq (process-status process) 'run))))
  814.  
  815. (defun xscheme-process-buffer ()
  816.   (let ((process (get-process xscheme-process-name)))
  817.     (and process (process-buffer process))))
  818.  
  819. (defun xscheme-process-buffer-window ()
  820.   (let ((buffer (xscheme-process-buffer)))
  821.     (and buffer (get-buffer-window buffer))))
  822.  
  823. (defun xscheme-process-buffer-current-p ()
  824.   "True iff the current buffer is the Scheme process buffer."
  825.   (eq (xscheme-process-buffer) (current-buffer)))
  826.  
  827. ;;;; Process Filter
  828.  
  829. (defun xscheme-process-sentinel (proc reason)
  830.   (let* ((buffer (process-buffer proc))
  831.      (name (buffer-name buffer)))
  832.     (save-excursion
  833.       (set-buffer buffer)
  834.       (xscheme-process-filter-initialize (eq reason 'run))
  835.       (if (not (eq reason 'run))
  836.     (progn
  837.       (setq scheme-mode-line-process "")
  838.       (setq xscheme-mode-string "no process")
  839.       (if (equal name (default-value 'xscheme-buffer-name))
  840.           (setq-default xscheme-runlight ""))))
  841.       (if (and (not (memq reason '(run stop)))
  842.            xscheme-signal-death-message)
  843.       (progn (beep)
  844.          (message
  845. "The Scheme process has died!  Do M-x reset-scheme to restart it"))))))
  846.  
  847. (defun xscheme-process-filter-initialize (running-p)
  848.   (setq xscheme-process-filter-state 'idle)
  849.   (setq xscheme-running-p running-p)
  850.   (setq xscheme-control-g-disabled-p nil)
  851.   (setq xscheme-allow-output-p t)
  852.   (setq xscheme-prompt "")
  853.   (if running-p
  854.       (let ((name (buffer-name (current-buffer))))
  855.     (setq scheme-mode-line-process '(": " xscheme-runlight-string))
  856.     (xscheme-modeline-initialize name)
  857.     (if (equal name (default-value 'xscheme-buffer-name))
  858.         (setq-default xscheme-runlight default-xscheme-runlight))))
  859.   (if (or (eq xscheme-runlight default-xscheme-runlight)
  860.       (equal xscheme-runlight ""))
  861.       (setq xscheme-runlight (list ": " 'xscheme-buffer-name ": " "?")))
  862.   (rplaca (nthcdr 3 xscheme-runlight)
  863.       (if running-p "?" "no process")))
  864.  
  865. (defun xscheme-process-filter (proc string)
  866.   (save-excursion
  867.     (set-buffer (process-buffer proc))
  868.     (let ((xscheme-filter-input string))
  869.       (while xscheme-filter-input
  870.     (cond ((eq xscheme-process-filter-state 'idle)
  871.            (let ((start (string-match "\e" xscheme-filter-input)))
  872.          (if start
  873.              (progn
  874.                (xscheme-process-filter-output
  875.             (substring xscheme-filter-input 0 start))
  876.                (setq xscheme-filter-input
  877.                  (substring xscheme-filter-input (1+ start)))
  878.                (setq xscheme-process-filter-state 'reading-type))
  879.            (let ((string xscheme-filter-input))
  880.              (setq xscheme-filter-input nil)
  881.              (xscheme-process-filter-output string)))))
  882.           ((eq xscheme-process-filter-state 'reading-type)
  883.            (if (zerop (length xscheme-filter-input))
  884.            (setq xscheme-filter-input nil)
  885.          (let ((char (aref xscheme-filter-input 0)))
  886.            (setq xscheme-filter-input
  887.              (substring xscheme-filter-input 1))
  888.            (let ((entry (assoc char xscheme-process-filter-alist)))
  889.              (if entry
  890.              (funcall (nth 2 entry) (nth 1 entry))
  891.                (progn
  892.              (xscheme-process-filter-output ?\e char)
  893.              (setq xscheme-process-filter-state 'idle)))))))
  894.           ((eq xscheme-process-filter-state 'reading-string)
  895.            (let ((start (string-match "\e" xscheme-filter-input)))
  896.          (if start
  897.              (let ((string
  898.                 (concat xscheme-string-accumulator
  899.                     (substring xscheme-filter-input 0 start))))
  900.                (setq xscheme-filter-input
  901.                  (substring xscheme-filter-input (1+ start)))
  902.                (setq xscheme-process-filter-state 'idle)
  903.                (funcall xscheme-string-receiver string))
  904.            (progn
  905.              (setq xscheme-string-accumulator
  906.                (concat xscheme-string-accumulator
  907.                    xscheme-filter-input))
  908.              (setq xscheme-filter-input nil)))))
  909.           (t
  910.            (error "Scheme process filter -- bad state")))))))
  911.  
  912. ;;;; Process Filter Output
  913.  
  914. (defun xscheme-process-filter-output (&rest args)
  915.   (if xscheme-allow-output-p
  916.       (let ((string (apply 'concat args)))
  917.     (save-excursion
  918.       (xscheme-goto-output-point)
  919.       (while (string-match "\\(\007\\|\f\\)" string)
  920.         (let ((start (match-beginning 0))
  921.           (end (match-end 0)))
  922.           (insert-before-markers (substring string 0 start))
  923.           (if (= ?\f (aref string start))
  924.           (progn
  925.             (if (not (bolp))
  926.             (insert-before-markers ?\n))
  927.             (insert-before-markers ?\f))
  928.           (beep))
  929.           (setq string (substring string (1+ start)))))
  930.       (insert-before-markers string)))))
  931.  
  932. (defun xscheme-guarantee-newlines (n)
  933.   (if xscheme-allow-output-p
  934.       (save-excursion
  935.     (xscheme-goto-output-point)
  936.     (let ((stop nil))
  937.       (while (and (not stop)
  938.               (bolp))
  939.         (setq n (1- n))
  940.         (if (bobp)
  941.         (setq stop t)
  942.         (backward-char))))
  943.     (xscheme-goto-output-point)
  944.     (while (> n 0)
  945.       (insert-before-markers ?\n)
  946.       (setq n (1- n))))))
  947.  
  948. (defun xscheme-goto-output-point ()
  949.   (let ((process (get-process xscheme-process-name)))
  950.     (set-buffer (process-buffer process))
  951.     (goto-char (process-mark process))))
  952.  
  953. (defun xscheme-modeline-initialize (name)
  954.   (setq xscheme-runlight-string "")
  955.   (if (equal name (default-value 'xscheme-buffer-name))
  956.       (setq-default xscheme-runlight-string ""))
  957.   (setq xscheme-mode-string "")
  958.   (setq mode-line-buffer-identification
  959.     (list (concat name ": ")
  960.           'xscheme-mode-string)))
  961.  
  962. (defun xscheme-set-runlight (runlight)
  963.   (setq xscheme-runlight-string runlight)
  964.   (if (equal (buffer-name (current-buffer))
  965.          (default-value 'xscheme-buffer-name))
  966.       (setq-default xscheme-runlight-string runlight))
  967.   (rplaca (nthcdr 3 xscheme-runlight) runlight)
  968.   (xscheme-modeline-redisplay))
  969.  
  970. (defun xscheme-modeline-redisplay ()
  971.   (save-excursion (set-buffer (other-buffer)))
  972.   (set-buffer-modified-p (buffer-modified-p))
  973.   (sit-for 0))
  974.  
  975. ;;;; Process Filter Operations
  976.  
  977. (defvar xscheme-process-filter-alist
  978.   '((?D xscheme-enter-debugger-mode
  979.     xscheme-process-filter:string-action)
  980.     (?E xscheme-eval
  981.     xscheme-process-filter:string-action)
  982.     (?P xscheme-set-prompt-variable
  983.     xscheme-process-filter:string-action)
  984.     (?R xscheme-enter-interaction-mode
  985.     xscheme-process-filter:simple-action)
  986.     (?b xscheme-start-gc
  987.     xscheme-process-filter:simple-action)
  988.     (?e xscheme-finish-gc
  989.     xscheme-process-filter:simple-action)
  990.     (?f xscheme-exit-input-wait
  991.     xscheme-process-filter:simple-action)
  992.     (?g xscheme-enable-control-g
  993.     xscheme-process-filter:simple-action)
  994.     (?i xscheme-prompt-for-expression
  995.     xscheme-process-filter:string-action)
  996.     (?m xscheme-message
  997.     xscheme-process-filter:string-action)
  998.     (?n xscheme-prompt-for-confirmation
  999.     xscheme-process-filter:string-action)
  1000.     (?o xscheme-output-goto
  1001.     xscheme-process-filter:simple-action)
  1002.     (?p xscheme-set-prompt
  1003.     xscheme-process-filter:string-action)
  1004.     (?s xscheme-enter-input-wait
  1005.     xscheme-process-filter:simple-action)
  1006.     (?v xscheme-write-value
  1007.     xscheme-process-filter:string-action)
  1008.     (?w xscheme-cd
  1009.     xscheme-process-filter:string-action)
  1010.     (?z xscheme-display-process-buffer
  1011.     xscheme-process-filter:simple-action)
  1012.     (?c xscheme-unsolicited-read-char
  1013.     xscheme-process-filter:simple-action))
  1014.   "Table used to decide how to handle process filter commands.
  1015. Value is a list of entries, each entry is a list of three items.
  1016.  
  1017. The first item is the character that the process filter dispatches on.
  1018. The second item is the action to be taken, a function.
  1019. The third item is the handler for the entry, a function.
  1020.  
  1021. When the process filter sees a command whose character matches a
  1022. particular entry, it calls the handler with two arguments: the action
  1023. and the string containing the rest of the process filter's input
  1024. stream.  It is the responsibility of the handler to invoke the action
  1025. with the appropriate arguments, and to reenter the process filter with
  1026. the remaining input.")
  1027.  
  1028. (defun xscheme-process-filter:simple-action (action)
  1029.   (setq xscheme-process-filter-state 'idle)
  1030.   (funcall action))
  1031.  
  1032. (defun xscheme-process-filter:string-action (action)
  1033.   (setq xscheme-string-receiver action)
  1034.   (setq xscheme-string-accumulator "")
  1035.   (setq xscheme-process-filter-state 'reading-string))
  1036.  
  1037. (defconst xscheme-runlight:running "run"
  1038.   "The character displayed when the Scheme process is running.")
  1039.  
  1040. (defconst xscheme-runlight:input "input"
  1041.   "The character displayed when the Scheme process is waiting for input.")
  1042.  
  1043. (defconst xscheme-runlight:gc "gc"
  1044.   "The character displayed when the Scheme process is garbage collecting.")
  1045.  
  1046. (defun xscheme-start-gc ()
  1047.   (xscheme-set-runlight xscheme-runlight:gc))
  1048.  
  1049. (defun xscheme-finish-gc ()
  1050.   (xscheme-set-runlight
  1051.    (if xscheme-running-p xscheme-runlight:running xscheme-runlight:input)))
  1052.  
  1053. (defun xscheme-enter-input-wait ()
  1054.   (xscheme-set-runlight xscheme-runlight:input)
  1055.   (setq xscheme-control-g-disabled-p nil)
  1056.   (setq xscheme-running-p nil))
  1057.  
  1058. (defun xscheme-exit-input-wait ()
  1059.   (xscheme-set-runlight xscheme-runlight:running)
  1060.   (setq xscheme-running-p t))
  1061.  
  1062. (defun xscheme-enable-control-g ()
  1063.   (setq xscheme-control-g-disabled-p nil))
  1064.  
  1065. (defun xscheme-display-process-buffer ()
  1066.   (let ((window (or (xscheme-process-buffer-window)
  1067.             (display-buffer (xscheme-process-buffer)))))
  1068.     (save-window-excursion
  1069.       (select-window window)
  1070.       (xscheme-goto-output-point)
  1071.       (if (xscheme-debugger-mode-p)
  1072.       (xscheme-enter-interaction-mode)))))
  1073.  
  1074. (defun xscheme-unsolicited-read-char ()
  1075.   nil)
  1076.  
  1077. (defun xscheme-eval (string)
  1078.   (eval (car (read-from-string string))))
  1079.  
  1080. (defun xscheme-message (string)
  1081.   (if (not (zerop (length string)))
  1082.       (xscheme-write-message-1 string (format ";%s" string))))
  1083.  
  1084. (defun xscheme-write-value (string)
  1085.   (if (zerop (length string))
  1086.       (xscheme-write-message-1 "(no value)" ";No value")
  1087.       (xscheme-write-message-1 string (format ";Value: %s" string))))
  1088.  
  1089. (defun xscheme-write-message-1 (message-string output-string)
  1090.   (let* ((process (get-process xscheme-process-name))
  1091.      (window (get-buffer-window (process-buffer process))))
  1092.     (if (or (not window)
  1093.         (not (pos-visible-in-window-p (process-mark process)
  1094.                       window)))
  1095.     (message "%s" message-string)))
  1096.   (xscheme-guarantee-newlines 1)
  1097.   (xscheme-process-filter-output output-string))
  1098.  
  1099. (defun xscheme-set-prompt-variable (string)
  1100.   (setq xscheme-prompt string))
  1101.  
  1102. (defun xscheme-set-prompt (string)
  1103.   (setq xscheme-prompt string)
  1104.   (xscheme-guarantee-newlines 2)
  1105.   (setq xscheme-mode-string (xscheme-coerce-prompt string))
  1106.   (xscheme-modeline-redisplay))
  1107.  
  1108. (defun xscheme-output-goto ()
  1109.   (xscheme-goto-output-point)
  1110.   (xscheme-guarantee-newlines 2))
  1111.  
  1112. (defun xscheme-coerce-prompt (string)
  1113.   (if (string-match "^[0-9]+ \\[[^]]+\\] " string)
  1114.       (let ((end (match-end 0)))
  1115.     (let ((prompt (substring string end)))
  1116.       (xscheme-process-filter-output prompt)
  1117.       (if (and (> (length prompt) 0)
  1118.            (not (= (aref prompt (- (length prompt) 1)) ? )))
  1119.           (xscheme-process-filter-output " "))
  1120.       (substring string 0 (- end 1))))
  1121.       string))
  1122.  
  1123. (defun xscheme-cd (directory-string)
  1124.   (save-excursion
  1125.     (set-buffer (xscheme-process-buffer))
  1126.     (cd directory-string)))
  1127.  
  1128. (defun xscheme-prompt-for-confirmation (prompt-string)
  1129.   (xscheme-send-char (if (y-or-n-p prompt-string) ?y ?n)))
  1130.  
  1131. (defun xscheme-prompt-for-expression (prompt-string)
  1132.   (xscheme-send-string-2
  1133.    (read-from-minibuffer prompt-string nil xscheme-prompt-for-expression-map)))
  1134.  
  1135. (defvar xscheme-prompt-for-expression-map nil)
  1136. (if (not xscheme-prompt-for-expression-map)
  1137.     (progn
  1138.       (setq xscheme-prompt-for-expression-map
  1139.         (copy-keymap minibuffer-local-map))
  1140.       (substitute-key-definition 'exit-minibuffer
  1141.                  'xscheme-prompt-for-expression-exit
  1142.                  xscheme-prompt-for-expression-map)))
  1143.  
  1144. (defun xscheme-prompt-for-expression-exit ()
  1145.   (interactive)
  1146.   (if (eq (xscheme-region-expression-p (point-min) (point-max)) 'one)
  1147.       (exit-minibuffer)
  1148.       (error "input must be a single, complete expression")))
  1149.  
  1150. (defun xscheme-region-expression-p (start end)
  1151.   (save-excursion
  1152.     (let ((old-syntax-table (syntax-table)))
  1153.       (unwind-protect
  1154.       (progn
  1155.         (set-syntax-table scheme-mode-syntax-table)
  1156.         (let ((state (parse-partial-sexp start end)))
  1157.           (and (zerop (car state))    ;depth = 0
  1158.            (nth 2 state)    ;last-sexp exists, i.e. >= 1 sexps
  1159.            (let ((state (parse-partial-sexp start (nth 2 state))))
  1160.              (if (nth 2 state) 'many 'one)))))
  1161.     (set-syntax-table old-syntax-table)))))
  1162.